<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class omnitrix_html_response extends omnitrix_html_container
{
	protected $_title;
	protected $_breadcrumbs = array();
	protected $_messages = array();
	protected $_navtabs = array();
	//protected $_body = array();

	public function set_title($title)
	{
		$this->_title = $title;
		return $this;
	}

	public function add_breadcrumb($text, $link = null)
	{
		$this->_breadcrumbs[] = array(
				'text' => $text,
				'link' => $link
		);
		return $this;
	}

	public function add_navtab($title, $description, $link, $active = false)
	{
		$this->_navtabs[] = array(
				'title' => $title,
				'description' => $description,
				'link' => $link,
				'active' => $active
		);
		return $this;
	}

	public function create_message($class = null)
	{
		$message = otx_html_tag_custom('message', $class);
		$this->_messages[] = $message;
		return $message;
	}

	public function create_wrapper($form_action = null, $form_method = null, $submit_method = true)
	{
		$wrapper = otx_html_tag_custom('wrapper', $form_action, $form_method, $submit_method);
		$this->add_child($wrapper);
		return $wrapper;
	}

	public function create_table($title = null, $class = null, $pager_info = null, $link_pattern = null)
	{
		$table = otx_html_tag_custom('table', $title, $class, $pager_info, $link_pattern);
		$this->add_child($table);
		return $table;
	}

// 	public function append_body($content, $name = '_default_')
// 	{
// 		if (isset($this->_body[$name])) {
// 			$this->_body[$name] .= $content;
// 		} else {
// 			$this->_body[$name] = $content;
// 		}
// 		return $this;
// 	}

	public function render_html()
	{
		$tpl_vars['title'] = $this->_title;
		$tpl_vars['response_html'] = $this->_render_breadcrumbs()
				. $this->_render_flash()
				. $this->_render_messages()
				. $this->_render_navtabs()
				. parent::render_html();
		return otx_template_parse('index', $tpl_vars, otx_module_path('admin', 'templates/'));
	}

	protected function _render_breadcrumbs()
	{
		if (empty($this->_breadcrumbs)) {
			return '';
		}

		if (!otx_session_is_admin()) {
			return '';
		}

		$html = otx_html_tag_start('div', array('id' => 'breadcrumbs'));

		for ($i = 0; $i < count($this->_breadcrumbs); $i++) {
			$html .= otx_html_tag_start('span', array('class' => 'breadcrumb' . (empty($this->_breadcrumbs[$i]['link']) ? ' active' : '')));

			if (!empty($this->_breadcrumbs[$i]['link'])) {
				$html .= otx_html_tag_start('a', array('href' => $this->_breadcrumbs[$i]['link']))
						. $this->_breadcrumbs[$i]['text']
						. otx_html_tag_end('a');
			} else {
				$html .= otx_html_strong($this->_breadcrumbs[$i]['text']);
			}

			$html .= otx_html_tag_end('span');

			if ($i < count($this->_breadcrumbs) - 1) {
				$html .= otx_html_tag_start('span', array('class' => 'divider'))
						. '&raquo;'
						. otx_html_tag_end('span');
			}
		}

// 		if (!empty($this->_title)) {
// 			$html .= otx_html_tag_start('span', array('class' => 'divider'))
// 					. '&raquo;'
// 					. otx_html_tag_end('span')
// 					. otx_html_tag_start('span', array('class' => 'breadcrumb active'))
// 					. $this->_title
// 					. otx_html_tag_end('span');
// 		}

		$html .= otx_html_tag_end('div');

		return $html;
	}

	protected function _render_flash()
	{
		if (empty($_SESSION['_XFLASH'])) {
			unset($_SESSION['_XFLASH']);
			return '';
		}

		$html = '';

		foreach ((array) $_SESSION['_XFLASH'] as $message) {
			$html .= otx_html_tag_simple('div', $message['text'], array('class' => $message['type']));
		}

		unset($_SESSION['_XFLASH']);
		return $html;
	}

	protected function _render_messages()
	{
		if (empty($this->_messages)) {
			return '';
		}

		$html = '';

		foreach ($this->_messages as $message) {
			$html .= (string) $message;
		}

		return $html;
	}

	protected function _render_navtabs()
	{
		if (empty($this->_navtabs)) {
			return '';
		}

		$active_navtab = false;

		for ($i = 0; $i < count($this->_navtabs); $i++) {
			if ($this->_navtabs[$i]['active']) {
				$active_navtab = $i;
				break;
			}
		}

		if (false === $active_navtab) {
			$this->_navtabs[0]['active'] = true;
			$active_navtab = 0;
		}

		$html = otx_html_tag_start('div', array('id' => 'navtabs'))
				. otx_html_tag_start('ul');

		for ($i = 0; $i < count($this->_navtabs); $i++) {
			if ($i == $active_navtab) {
				$li_attribs = array('class' => 'active');
			} else {
				$li_attribs = array();
			}
			$html .= otx_html_tag_start('li', $li_attribs)
					. otx_html_tag_start('a', array('href' => $this->_navtabs[$i]['link']))
					. $this->_navtabs[$i]['title']
					. otx_html_tag_end('a')
					. otx_html_tag_end('li');
		}

		$html .= otx_html_tag_end('ul');

		$html .= otx_html_tag_start('div', array('class' => 'description'))
				. $this->_navtabs[$active_navtab]['description']
				. otx_html_tag_end('div');

		$html .= otx_html_tag_end('div');

		return $html;
	}
}